Solutions for Comp248 Assignment 5

#ifndef REPORT_H 
#define REPORT_H  

#include<fstream.h>
#include<iostream.h>
#include"tstring.h"
#include"iomanip.h"
#include"textlib.h"

 
class Report
{	
	private:
	
	String  fileName;	
	//fileName is passed as a string object
	
	int numEntries;		
	//the number of entries is read at the beginning of file
	//however, there is another way of doing it

	public:
		
	Report(String file);	
	//the constructor initializes the file name

	void filesOp();			
	//all the files operations are performed here
	
	double totalIt( int As1, int As2 , int MidT , int Final);
	//the marks are totaled thru this function

	String getGrade(double function);
	//the grades are figured out here

};



Report::Report(String file):fileName(file)	//the constructor is implemented here
{}


void Report::filesOp()	//file operations are implemented here
{
	
	String fileIn,fileOut;
	ifstream fin;
	ofstream fout;

	

	fileIn=fileName;

	fileOut=fileIn+".output";	
	//Concatenation is used here

	fin.open(fileIn.c_str(), ios::in|ios::nocreate);
	//making sure that no file with zero lenght is read from

	fout.open(fileOut.c_str());	//opening the output files



	if(!fin)	//if an error is detected then the program is automaticalled exited
	{
		cerr<<" Failed to Open "<<fileName<<endl;
		exit(1);
	}
	else	//otherwise operations are performed normally
	{
		cout<<endl;
		cout<<" Input file   : "<<fileIn<<" "<<endl;
		cout<<" Output file  : "<<fileOut<<" "<<endl;

		for(int i=0;i<=3;i++)
			{
				for(int j=0;j<=i;j++)
				{
					cout<<".";
				}
			}
	cout<<endl;
	cout<<endl;
	}

	String ID;
	int A1,A2,MT, FE;	//declaring the identifiers

	
	//formatting the output
	fout<<" File's Name: "<<fileOut<<endl<<endl;	//writting file's name
	fout<<"  ID\t "<<" A1\t"
		<<"A2\t "<<" MT\t"<<"FE\t "<<"Total\t"<<"Grade"
		<<endl;

	fout<<"---------------------------------------------"<<endl;
		
	fin>>numEntries;	//reading the first line in the opened input file

	double sumA1=0.0,sumA2=0.0,sumMT=0.0,
		   sumFE=0.0,sumTot=0.0;
	int count=0;
	while(count != numEntries)
	{
		fin>> ID>>A1>>A2>>MT>>FE;
		fout<<ID<<setw(6)<<A1<<setw(6)<<A2<<setw(6)<<MT
			<<setw(6)<<FE<<setreal(8,1)<<totalIt(A1,A2,MT,FE)
			<<'\t'<<"  "
			<<getGrade(totalIt(A1,A2,MT,FE))
			<<endl;

		sumA1+=A1;
		sumA2+=A2;
		sumMT+=MT;
		sumFE+=FE;
		sumTot+=totalIt(A1,A2,MT,FE);

		count++;

	}

	fout<<"---------------------------------------------"<<endl;
	fout<<"Averages:"<<setreal(5,1)<<sumA1/numEntries
		<<setreal(6,1)<<sumA2/numEntries<<setreal(6,1)
		<<sumMT/numEntries<<setreal(6,1)<<sumFE/numEntries
		<<setreal(6,1)<<sumTot/numEntries<<endl;

	
}


double Report::totalIt(int As1,int As2,int MidT,int Final)
{	//implementing the total function here
	
	double T;
    
	T=(As1*(1.0/2.0))+(As2*(1.0/3.0))+
	  (MidT*(2.0/10.0))+(Final*(6.0/10.0));

	return T;
}


String Report::getGrade(double function)
{	//implementing evluation of grade here
	 String GRADES[13]={"F",
						"D-","D","D+",
						"C-","C","C+",
						"B-","B","B+",
						"A-","A","A+"};
	 //an array is created to reference grades
	int i=0; 	
	int mark= int (function);	//converting the function from double to integer
	
	if (mark<=46)
	{
		GRADES[i];	//i is still zero
	}
	else
	{
		i=((mark-42)/4);
		GRADES[i];
	}

	return GRADES[i];
}


#endif